package cn.xuan.zp.service.resource.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.xuan.zp.db.domain.Resource;
import cn.xuan.zp.db.domain.ResourceExample;
import cn.xuan.zp.db.domain.Role;
import cn.xuan.zp.db.domain.RoleResource;
import cn.xuan.zp.db.domain.RoleResourceExample;
import cn.xuan.zp.db.mapper.ResourceMapper;
import cn.xuan.zp.db.mapper.RoleResourceMapper;
import cn.xuan.zp.service.resource.IResourceService;

@Service
public class ResourceServiceImpl implements IResourceService {

	@Autowired
	RoleResourceMapper roleResourceMapper;
	@Autowired
	ResourceMapper resourceMapper;
	
	@Override
	public int addEnity(Resource enity) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateEnity(Resource enity) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Resource getByPrimaryKey(long id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object queryAll() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object queryEnity(Object... params) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Resource> queryResourceByRoles(List<Role> roles) throws Exception {
		List<Resource> resources = new ArrayList<>();
		List<Long> values = new ArrayList<>();
		for (Role role : roles) {
			values.add(role.getId());
		}

		if(values != null && values.size() > 0) {
			RoleResourceExample exampleRoleResource = new RoleResourceExample();
			
			cn.xuan.zp.db.domain.RoleResourceExample.Criteria criteriaRoleResource = exampleRoleResource.createCriteria();
			
			criteriaRoleResource.andRoleIdIn(values );
			
			List<RoleResource> lst = roleResourceMapper.selectByExample(exampleRoleResource );
			
			if(lst != null && lst.size() > 0) {
				List<Long> ids = new ArrayList<>();
				for (RoleResource roleResource : lst) {
					ids.add(roleResource.getResourceId());
				}
				
				ResourceExample example = new ResourceExample();
				
				cn.xuan.zp.db.domain.ResourceExample.Criteria criteria = example.createCriteria();
				
				criteria.andIdIn(ids);
				
				resources = resourceMapper.selectByExample(example );
			}
		}		
		
		return resources;
	}

}
